package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.UserInfoDao;
import com.vp.org.pojo.UserInfo;
import com.vp.org.util.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;


public class UserInfoDaoImpl extends JdbcUtil implements Sql, UserInfoDao {


    // 添加用户(注册用户)
    @Override
    public int addUser(UserInfo userInfo) {
        return template.update(USERINFO_ADDUSER,
                userInfo.getNickname(),userInfo.getPassword(),userInfo.getPhone(),userInfo.getQq(),
                userInfo.getUsername(),userInfo.getAddress(),userInfo.getCity(),userInfo.getDistrict(),
                userInfo.getClassname(),userInfo.getGrade(),userInfo.getFamilyPhone(),userInfo.getPostcode(),
                userInfo.getProvince(),userInfo.getType(),userInfo.getSchool(), userInfo.getIntegral(),
                userInfo.getPicture(),userInfo.getEmail(),userInfo.getHobby(), userInfo.getIntroduce(),
                userInfo.getBirthday(),userInfo.getGender());
    }

    // 用户信息修改
    @Override
    public int updateUserInfo(UserInfo userInfo) {
        return template.update(USERINFO_UPDATEUSERINFO,
                userInfo.getNickname(),userInfo.getPassword(),userInfo.getPhone(),userInfo.getQq(),
                userInfo.getUsername(),userInfo.getAddress(),userInfo.getCity(),userInfo.getDistrict(),
                userInfo.getClassname(),userInfo.getGrade(),userInfo.getFamilyPhone(),userInfo.getPostcode(),
                userInfo.getProvince(),userInfo.getType(),userInfo.getSchool(), userInfo.getIntegral(),
                userInfo.getPicture(),userInfo.getEmail(),userInfo.getHobby(), userInfo.getIntroduce(),
                userInfo.getBirthday(),userInfo.getGender(),userInfo.getUserId());
    }

    // 登录验证
    @Override
    public int loginVerify(UserInfo userInfo) {
        return template.queryForList(USERINFO_LOGINVERIFY,userInfo.getPhone(),userInfo.getPassword()).size();
    }

    // 用户共享数据
    @Override
    public UserInfo shareData(UserInfo userInfo) {
        return template.queryForObject(USERINFO_SHAREDATA,
                new BeanPropertyRowMapper<>(UserInfo.class),
                userInfo.getPhone(),userInfo.getPassword());

    }

    // 注册验证
    @Override
    public int registerVerify(UserInfo userInfo) {
        return template.queryForList(USERINFO_REGISTERVERIFY,userInfo.getPhone()).size();

    }

    // 后台注册用户
    // insert into user_info (phone,password,email,family_phone,qq) values (?,?,?,?,?);
    @Override
    public int login(UserInfo userInfo) {
        return template.update(USER_ADD,userInfo.getPhone(),userInfo.getPassword(),
                userInfo.getEmail(),userInfo.getFamilyPhone(),userInfo.getQq(),userInfo.getIntegral());
    }

    // 后台删除用户
    // delete from user_info where phone = ?;
    @Override
    public int delete(UserInfo userInfo) {
        return template.update(USER_DELETE,userInfo.getPhone());
    }

    // 后台总数
    // select count(user_id) count from user_info;
    @Override
    public long count() {
        return (long) template.queryForMap(USER_COUNT).get("count");
    }

    // 后台管理员总页数
    @Override
    public int page() {
        int count = (int) count();
        return count % 3 == 0 ? count / 3 : count / 3 + 1;
    }

    // 管理员分页查询所有用户
    // select * from user_info limit 0,3;
    @Override
    public List<UserInfo> selectUserInfo(int page) {
        return template.query(USER_PAGE,new BeanPropertyRowMapper<>(UserInfo.class),(page - 1) * 3);
    }

}
